package com.takeshequ.homepage;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import net.tsz.afinal.FinalBitmap;
import net.tsz.afinal.FinalHttp;
import net.tsz.afinal.annotation.view.ViewInject;
import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.text.SpannableString;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.takeshequ.BasicActivity;
import com.takeshequ.Constants;
import com.takeshequ.R;
import com.takeshequ.chat.ExpressionUtil;
import com.takeshequ.chat.TalkActivity;
import com.takeshequ.chat.TalkHistory;
import com.takeshequ.feeds.FeedsDetailActivity;
import com.takeshequ.friends.BaseData;
import com.takeshequ.friends.Friend;
import com.takeshequ.main.MainFragmentActivity;
import com.utils.HttpClientUtils;
import com.utils.HttpClientUtils.HttpResponseHandler;

public class HomePageActivity extends BasicActivity {
	@ViewInject(id = R.id.homepage_back, click = "back")
	private Button back;
	@ViewInject(id = R.id.feeds_list_view_process)
	private View processView;
	@ViewInject(id = R.id.homepage_headimg)
	private ImageView headImg;
	@ViewInject(id = R.id.homepage_age)
	private TextView ageView;
	@ViewInject(id = R.id.homepage_place)
	private TextView placeView;

	@ViewInject(id = R.id.homepage_my_car)
	private TextView myCarView;

	@ViewInject(id = R.id.homepage_favorite_car)
	private TextView favoriteCarView;
	@ViewInject(id = R.id.homepage_lastbehavior)
	private TextView lastBehaviorView;

	@ViewInject(id = R.id.homepage_lastbehavior_view, click = "feedDetail")
	private View lastBehaviorLayoutView;

	@ViewInject(id = R.id.homepage_signature)
	private TextView signatureView;

	@ViewInject(id = R.id.homepage_constellation)
	private TextView constellationView;

	@ViewInject(id = R.id.homepage_takenumber)
	private TextView takeNumberView;

	@ViewInject(id = R.id.homepage_job)
	private TextView jobView;

	@ViewInject(id = R.id.homepage_nickName)
	private TextView nickNameView;

	@ViewInject(id = R.id.homepage_hobby)
	private TextView hobbyView;

	@ViewInject(id = R.id.homepage_regDate)
	private TextView regDateView;

	@ViewInject(id = R.id.homepage_bottom)
	private View bottomView;

	@ViewInject(id = R.id.homepage_talk_btn, click = "talkTo")
	private ImageView talkTo;

	@ViewInject(id = R.id.homepage_edit_btn, click = "edit")
	private ImageButton editBtn;

	@ViewInject(id = R.id.homepage_report_btn, click = "report")
	private ImageView reportBtn;

	@ViewInject(id = R.id.homepage_add_friend_btn, click = "addFriend")
	private ImageView addFriendBtn;
	private static SimpleDateFormat sf;
	private Member member;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.homepage);
		sf = new SimpleDateFormat("yyyy年MM月dd日  HH:mm:ss");
	}

	@Override
	protected void onResume() {
		super.onResume();
		processView.setVisibility(View.VISIBLE);
		if (Constants.selectMemberId.compareTo(Constants.memberId) == 0) {
			bottomView.setVisibility(View.GONE);
			editBtn.setVisibility(View.VISIBLE);
		} else {
			bottomView.setVisibility(View.VISIBLE);
			editBtn.setVisibility(View.GONE);
		}

		if (!(BaseData.getFriend(Constants.selectMemberId) instanceof Friend)) {
			talkTo.setVisibility(View.GONE);
		} else {
			addFriendBtn.setVisibility(View.GONE);
		}
		HttpClientUtils.getPersonalInfo(new HttpResponseHandler() {

			@Override
			public void handler(String response) {
				System.out.println(response);
				try {
					JSONObject jsonObject = new JSONObject(response);
					processView.setVisibility(View.GONE);
					Gson gson = new Gson();
					member = gson.fromJson(jsonObject.getString("member"),
							Member.class);
					FinalBitmap.create(HomePageActivity.this).display(headImg,
							member.getHeadImage());
					ageView.setText(member.getAge());
					myCarView.setText(member.getMyCars());
					favoriteCarView.setText(member.getLikeCars());
					lastBehaviorView.setText(member.getLastestBehavior());

					SpannableString spannableString = ExpressionUtil
							.getExpressionString(HomePageActivity.this,
									member.getSignature());
					signatureView.setText(spannableString);
					constellationView.setText(member.getConstellation());
					takeNumberView.setText(String.valueOf(member.getTakeId()));
					jobView.setText(member.getJob());
					hobbyView.setText(member.getInterest());
					regDateView.setText(member.getRegDateString());
					nickNameView.setText(member.getNickName());
					Constants.member = member;
				} catch (JSONException e) {
					e.printStackTrace();
				}

			}
		});
	}

	public void edit(View v) {
		Intent intent = new Intent(HomePageActivity.this,
				HomePageEditActivity.class);
		startActivity(intent);
		// Toast.makeText(HomePageActivity.this, "还在完善,请稍后...", 2000).show();
	}

	public void back(View v) {
		overridePendingTransition(R.anim.push_left_in, R.anim.push_right_out);
		startActivity(new Intent(HomePageActivity.this,
				MainFragmentActivity.class));
	}

	public void talkTo(View v) {
		Constants.firend.setMemberId(Constants.selectMemberId);
		Constants.firend.setNickName(member.getNickName());
		Constants.firend.setHeadImage(member.getHeadImage());
		startActivity(new Intent(HomePageActivity.this, TalkActivity.class));
	}

	public void addFriend(View v) {
		Map<String, String> params = new HashMap<String, String>();
		params.put("fromMemberId", Constants.memberId);
		params.put("toMemberId", member.getMemberId());
		HttpClientUtils.post("addFriend", params, new HttpResponseHandler() {

			@Override
			public void handler(String response) {
				Toast toast = Toast.makeText(HomePageActivity.this, "发送申请成功",
						3000);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
				FinalHttp httpClient = new FinalHttp();
				AjaxParams params = new AjaxParams();
				TalkHistory talkHistory = new TalkHistory();
				talkHistory.setContent(Constants.member.getNickName()
						+ "请求加您为好友.");
				talkHistory.setDate(sf.format(new Date()));
				talkHistory.setTargetId(Constants.selectMemberId);
				talkHistory.setFromId(Constants.memberId);
				params.put("message", gson.toJson(talkHistory));
				httpClient.post(Constants.domainAndPort + "/talk/single",
						params, new AjaxCallBack<String>() {
							@Override
							public void onFailure(Throwable t, String strMsg) {
								super.onFailure(t, strMsg);
							}

							@Override
							public void onSuccess(String t) {

							}
						});

			}
		});
	}

	public void feedDetail(View v) {
		Intent intent = new Intent(HomePageActivity.this,
				FeedsDetailActivity.class);
		intent.putExtra("behaviorId", member.getLastBehaviorId());
		startActivity(intent);
	}

	public void report(View v) {
		Toast.makeText(HomePageActivity.this, "已发送,等待审核", 3000).show();
	}
}
